/*
 * Copyright (C), 2002-2017, 你的兔宝宝电子商务有限公司
 * FileName: SimpleController.java
 * Author:   你的兔宝宝
 * Date:     2017-1-22 下午5:13:48
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.baby.demo.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baby.demo.utils.Collections;
import com.baby.demo.utils.Jsons;
import com.baby.demo.utils.Maps;
import com.baby.demo.utils.dal.DalClient;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author 你的兔宝宝
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/simple")
public class SimpleController {
    
    /**
     * 心跳返回信息
     */
    String heartWords = "it works!";
    
    @Resource
    DalClient dalClient;
    
    @RequestMapping("hearthit")
    @ResponseBody
    public String hearthit(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> dbtime = dalClient.queryForMap("sample.query4dbtime", Jsons.json2map("{dbType:'MYSQL'}"));
        return heartWords + dbtime;
    }
    
    @RequestMapping("queryShopShareActJsonp")
    @ResponseBody
    public JSONPObject test1(HttpServletRequest request, HttpServletResponse response){
        JSONPObject jsonp = new JSONPObject("helloworld", Jsons.json2map("{\"success\":true,\"activityCode\":\"1708241719541000001\",\"shareId\":\"7IzWLiEBANWf+ogSzWZyjEXgZhKl8ApcPG0RbRq7xfq5hj2IEyfzj9+WghN3Z3ZqaFNZ/K6BsZasLmiRtl9hPQ==\",\"desc\":\"分享店铺领2云钻，小伙伴们快来一起领取\",\"shareTitle\":\"分享就有礼，邀请小伙伴查看礼上加礼！\"}"));
        return jsonp;
    }
    
    @RequestMapping("add-play-records")
    @ResponseBody
    public JSONPObject addPlayRecords(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="params") Map<String,Object> params,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        if( Collections.isNotEmpty(params) ){
            try {
                int r = dalClient.execute("suning.yun.diamond.game.play.records.add", params);
                if( r > 0  ){
                    result.put("success", true);
                }else{
                    result.put("success", false);
                }
            } catch (Exception e) {
                result.put("success", false);
                result.put("msg", e.getMessage());
            }
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("analzye-play-records")
    @ResponseBody
    public JSONPObject analyzePlayRecords(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="callback") String callback){
        Map<String,Object> result = Maps.noop();
        Map<String,Object> params = Maps.noop();
        try {
            List<Map<String,Object>> r = dalClient.queryForList("suning.yun.diamond.game.play.records.analyze4count", params);
            result.put("rates", r);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
    
    @RequestMapping("query-play-records")
    @ResponseBody
    public JSONPObject queryPlayRecords(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value="callback") String callback,@RequestParam(value="params") Map<String,Object> params){
        Map<String,Object> result = Maps.noop();
        try {
            result = dalClient.queryForPage("suning.yun.diamond.game.play.records.query_records", params);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        JSONPObject jsonp = new JSONPObject(callback, result);
        return jsonp;
    }
}
